const pageStatistics = require("../entities/pageStatistics");
const { getButton, assignProporties, getSelect, getDatePicker, getSelect2, getTextBox } = require("../Common");

//statisticsManage/pageStatisticsList 1000-1099
const dataActionTypes = {
    //搜索查询
    searchQuery: 1000,
    //删除实体数据
    deleteEntityData: 1001,
    //Excel导出
    excelExport: 1002
};

const { name, primaryKey, viewName } = pageStatistics;
const entity = { name, primaryKey, viewName, expandMethods: { searchQuery: 'Select2' } };

module.exports = {
    name: "pageStatisticsList",
    type: "View",
    eventActions: getEventActions(),
    properties: assignProporties({ name: "pageStatisticsList" }, [getSearchOperationView(), getTabs()])
}

function getTabs() {
    return {
        name: 'tabs1',
        type: 'Tabs',
        className: 'divTabs',
        properties: assignProporties({ name: "pageStatisticsList" }, [getDataGridView(), getEchartsView()])
    }
}

function getEchartsView() {
    return {
        name: 'echartsView1',
        type: 'EchartsView',
        tabLabel: '曲线图',
        className: 'divEcharts'
    }
}

function getSearchOperationView() {
    return {
        name: "searchOperationView1",
        entity: entity,
        type: "RowsColsView",
        className: "divSerachView",
        properties: assignProporties({ name: "pageStatisticsList" }, [
            getEditSelect2("PageId", "页面", pageStatistics.pagesDataSource, 1, 1, 0, false),
            { ...getEditSelect("TimeType", "时间尺度", pageStatistics.timeTypeDataSource, 1, 2), defaultValue: 1 },
            { ...getDatePicker2("StartDate", "开始日期", 2, 2, "大于或等于其值"), isMonthFirst: true, operateLogic: ">=" },
            { ...getDatePicker2("EndDate", "至", 2, 3, "小于其值"), isCurrentDay: true, operateLogic: "<" },
            { ...getButton("search", "搜索", "primary", 2, 4), isFormItem: true, icon: "search", eventActionName: "searchQuery", pressEnterEventActionName: "searchQuery" },
            { ...getButton("clearQuery", "清空", "default", 2, 5), isFormItem: true, eventActionName: "clearQuery" },
        ])
    }
}

function getDatePicker2(name, label, x, y, placeHolder, defaultValue) {
    return {
        ...getDatePicker(name, label, x, y, defaultValue),
        isFormItem: true, colSpan: 6,
        isNullable: true,
        placeHolder: placeHolder,
        maxLength: 20,
        labelCol: 8,
        wrapperCol: 16,
        dataType: "DateTime",
        isCondition: true
    }
}

function getEditSelect2(name, label, serviceDataource, x, y, defaultValue, allowClear) {
    return {
        ...getSelect2(name, label, serviceDataource, x, y, defaultValue),
        isFormItem: true,
        colSpan: 6,
        labelCol: 8,
        wrapperCol: 16,
        operateLogic: "=",
        isSearch: true,
        isNullable: true,
        allowClear,
        isCondition: true
    }
}

function getTextBox2(name, label, x, y, contorlType, placeHolder, maxLength) {
    return {
        ...getTextBox(name, label, contorlType, x, y, placeHolder, maxLength || 50),
        isFormItem: true,
        colSpan: 6,
        labelCol: 8,
        wrapperCol: 16,
        isNullable: false,
        isCondition: true
    }
}

function getEditSelect(name, label, dataSource, x, y, defaultValue) {
    return {
        ...getSelect(name, label, dataSource, x, y, defaultValue),
        isFormItem: true,
        colSpan: 6,
        labelCol: 8,
        wrapperCol: 16,
        operateLogic: "=",
        isNullable: true,
        allowClear: false,
        isCondition: true
    }
}

function getDataGridView() {
    return {
        name: "dataGridView1",
        entity: entity,
        type: "DataGridView",
        entitySearchQuery: dataActionTypes.searchQuery,
        entityExcelExport: dataActionTypes.excelExport,
        eventActionName: "searchQuery",
        isDiv: true,
        className: "divInfoView3",
        isLocalPage: true,
        tabLabel: '数据列表',
        groupByInfoHtml: getGroupByInfoHtml(),
        properties: assignProporties(pageStatistics, [{ name: "TimePoint", orderByType: "desc", },
        { name: 'AvgMillSeconds', isVisible: false },
            'VisitCount', 'UserVisitCount', 'AvgVisitTime'])
    }
}

function getGroupByInfoHtml() {
    var html = [];

    html.push("总浏览量(PV)：<span style=\"color:#1890ff;\">{TotalVisitCount}</span>，");
    html.push("总访客量(UV)：<span style=\"color:#1890ff;\">{TotalUserVisitCount}</span>");

    return html.join("");
}

function getEventActions() {
    return [{
        name: "searchQuery",
        type: "dataGridView/searchQuery",
        searchView: "searchOperationView1",
        searchButton: "search",
        dataGridView: "dataGridView1",
        expandSearchQueryLoad: 'expandSearchQueryLoad'
    },
    {
        name: "clearQuery",
        type: "dataGridView/searchQuery",
        searchView: "searchOperationView1",
        searchButton: "clearQuery",
        dataGridView: "dataGridView1",
        isClearQuery: true,
        expandSearchQueryLoad: 'expandSearchQueryLoad'
    },
    {
        name: "excelExport",
        type: "dataGridView/excelExport",
        dataGridView: "dataGridView1"
    }]
}